﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KerriganInterface;
using KerriganDataContract;
using System.ServiceModel;
using System.Diagnostics;
using KerriganDataContract.Person;

/*
 * Class est appelé "PersonDAC" ==> Data Access Control.
 * Cette class regroupe tous les appels ver le EDMX
 */
namespace KerriganService.DAC
{
    public static class PersonDAC
    {

        public static PersonDC GetPerson(int id)
        {
            TPerson entity = new TPerson();
            PersonDC bo = new PersonDC();

            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.TPersons.FirstOrDefault(f => f.IdPerson == id);
                    //FormationBLL.CopyBoToEntity(bo, entity);
                    db.SaveChanges();
                    bo = PersonDAC.CopyEntityToBo(entity);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            return bo;
        }

        public static void UpdatePerson(PersonDC dc)
        {

        }

        public static void DeletePerson(int id)
        {

        }

        public static void CreatePerson(PersonDC dc)
        {

        }

        public static List<PersonDC> GetAllPerson()
        {
            List<PersonDC> res = new List<PersonDC>();

            PersonDC test = new PersonDC();
            test.IdPerson = 1;
            test.FirstName = "toto";

            res.Add(test);

            return res;
        }

        public static PersonDC CopyEntityToBo(TPerson entity)
        {
            PersonDC bo = new PersonDC();

            try
            {
                bo.IdPerson = entity.IdPerson;
                bo.LastName = entity.LastName;
                bo.FirstName = entity.FirstName;
                bo.NickName = entity.NickName;
                bo.Birthday = entity.Birthday;
                bo.PhoneNumber = entity.PhoneNumber;
                bo.Adress = entity.Adress;
                bo.PostCode = entity.PostCode;
                bo.City = entity.City;
                bo.Email = entity.Email;
                bo.Type = entity.IdPersonType;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }

        public static TPerson CopyBoToEntity(PersonDC bo)
        {
            TPerson entity = new TPerson();

            try
            {
                entity.IdPerson = bo.IdPerson;
                entity.LastName = bo.LastName;
                entity.FirstName = bo.FirstName;
                entity.NickName = bo.NickName;
                entity.Email = bo.Email;
                entity.Password = bo.Password;
                entity.Birthday = bo.Birthday;
                entity.PhoneNumber = bo.PhoneNumber;
                entity.Adress = bo.Adress;
                entity.PostCode = bo.PostCode;
                entity.City = bo.City;
                entity.IdPersonType = bo.Type;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return entity;
        }
    }
}
